-- Leviathan (c) v.v.balashoff
local Class = require('leviathan/Class')

local MouseWheel = Class:derive('UIMouseWheelSystem')

function MouseWheel:new(domain)
    self.name = 'MouseWheelSystem'
    self.domain = domain
end

function MouseWheel:update(dt)
    local entityWheel = nil -- entity with wheel component
    local mouse = nil
    local inputDomain = world:getDomain('Input')
    -- check is there any wheel component
    local ent = inputDomain.entities
    for i = 1, #ent do
        if ent[i]:getComponent('MouseWheelComponent') then
            entityWheel = ent[i]
        end
    end
    -- look entities in reverse order
    if entityWheel then
        local ent = self.domain.entities
        for i = #ent, 1, -1 do
            if ent[i].enabled then
                local sc = ent[i]:getComponent('SliderComponent')
                if sc then
                    local comp = entityWheel:getComponent('MouseWheelComponent')
                    -- handle UI part
                    sc.velocity = comp.y * love.graphics.getHeight() * 3
                    -- removing mouse wheel component in Input domain
                    entityWheel:deleteComponent(comp)
                end
            end
        end
    end
end

return MouseWheel
